﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Configuration;
using System.Data;
using Newtonsoft.Json;

namespace PromotionEngine
{
    public partial class DocLinePropertyDetail : System.Web.UI.Page
    {
        public class ResponseDetail
        {
            public List<Model.DocLinePropertyDetail> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public ResponseDetail(List<Model.DocLinePropertyDetail> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public ResponseDetail(string errors)
            {
                this.Errors = errors;
            }

            public ResponseDetail()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string propertyID = Request.QueryString["PropertyID"];
            string propertyCode = Request.QueryString["PropertyCode"];
            string propertyName = Request.QueryString["PropertyName"];

            this.lblPropertyCode.Text = propertyCode;
            this.lblPropertyName.Text = propertyName;
            this.lblPropertyID.Value = propertyID;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static ResponseDetail GetDocLinePropertyDetails(string propertyID)
        {
            List<Model.DocLinePropertyDetail> lstDocLinePropertyDetail = new List<Model.DocLinePropertyDetail>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllDocLinePropertyDetailByPropertyID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyID);
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.DocLinePropertyDetail docLinePropertyDetail = new Model.DocLinePropertyDetail();
                    docLinePropertyDetail.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    docLinePropertyDetail.PropertyDetailName = r["PropertyDetailName"].ToString();
                    docLinePropertyDetail.PropertyDetailID = r["PropertyDetailID"].ToString();
                    docLinePropertyDetail.PropertyID = r["PropertyID"].ToString();
                    docLinePropertyDetail.CreatedUserID = r["CreatedUserID"].ToString();

                    lstDocLinePropertyDetail.Add(docLinePropertyDetail);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstDocLinePropertyDetail };
                return new ResponseDetail(lstDocLinePropertyDetail, dsPromotion.Tables[0].Rows.Count);
            }
            return new ResponseDetail();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateDocLinePropertyDetail(string oDocLinePropertyDetail, bool isInsert)
        {
            Model.DocLinePropertyDetail docLinePropertyDetail = JsonConvert.DeserializeObject<Model.DocLinePropertyDetail>(oDocLinePropertyDetail);
            if (isInsert)
            {
                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        docLinePropertyDetail.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                    }
                }
            }
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonDocLinePropertyDetail(docLinePropertyDetail.PropertyID, docLinePropertyDetail.PropertyDetailCode, docLinePropertyDetail.PropertyDetailName
                                                    , docLinePropertyDetail.PropertyDetailID, docLinePropertyDetail.CreatedUserID,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteDocLinePropertyDetail(string propertyDetailID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteJsonDocLinePropertyDetail(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyDetailID);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
    }
}